package com.xiaoyu.client.ui.activity.help;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.discovery.DiscoveryRankAdapter;
import com.xiaoyu.client.adapter.discovery.NoticeAdapter;
import com.xiaoyu.client.adapter.discovery.ThankRecyAdapter;
import com.xiaoyu.client.model.discovery.DiscoveryRankingBean;
import com.xiaoyu.client.model.discovery.NoticeThankBean;
import com.xiaoyu.client.model.discovery.NoticeThankBean.DataBean.*;
import com.xiaoyu.client.model.discovery.RankParam;
import com.xiaoyu.client.model.seek.SeekParam;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumDetailActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.SpacesItemDecoration;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.ui.widget.NestedListView;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发现界面
 */
public class DiscoveryActivity extends BaseActivity implements View.OnClickListener {
    private ImageView backBtn;//返回
    private RecyclerView mNoticeLayout;//公告列表容器
    private RecyclerView thankLayout;//感谢榜单
    private NestedListView rankLayout;//日、周、月榜列表容器

    private AllPowerfulAdapter<NoticeBean> mNoticeAdapter;//公告适配器
    private ThankRecyAdapter mThankAdapter;//感谢榜单适配器

    private List<NoticeBean> mNoticeList = new ArrayList<>();
    private List<ThankBean> mThankList = new ArrayList<>();
    private Map<Integer,List<RankParam>> mList = new HashMap<>();

    private DiscoveryRankAdapter mRankAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discovery);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getNoticeThank();
        getRanking();
    }


    //初始化界面
    private void initView() {
        backBtn = findViewById(R.id.activity_discovery_title_back_btn);
        mNoticeLayout = findViewById(R.id.activity_discovery_notice_container);
        thankLayout = findViewById(R.id.activity_discovery_thank_layout);
        rankLayout = findViewById(R.id.activity_discovery_ranking_layout);

        initAdapter();

        backBtn.setOnClickListener(this);
    }

    private void initAdapter() {
        //公告适配器
        LinearLayoutManager noticeManager = new LinearLayoutManager(this);
        noticeManager.setOrientation(LinearLayoutManager.VERTICAL);
        mNoticeLayout.setLayoutManager(noticeManager);
        mNoticeLayout.addItemDecoration(new SpacesItemDecoration(UIUtils.dip2px(15),1));
        mNoticeAdapter = new AllPowerfulAdapter<NoticeBean>(R.layout.item_notice, mNoticeList, new AllPowerfulAdapter.OnClickListener<NoticeBean>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, NoticeBean noticeBean, int position) {
                Intent intent = new Intent(DiscoveryActivity.this, ForumDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(ClientConstants.FORUM_ID,noticeBean.getForumid());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }){
            @Override
            protected void convert(BaseViewHolder baseViewHolder, NoticeBean noticeBean) {
                super.convert(baseViewHolder, noticeBean);
                baseViewHolder.setText(R.id.item_notice_content,noticeBean.getFourmtitle());
            }
        };
        mNoticeLayout.setAdapter(mNoticeAdapter);

        //感谢榜单
        LinearLayoutManager thankManager = new LinearLayoutManager(this);
        thankManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        thankLayout.addItemDecoration(new SpacesItemDecoration(UIUtils.dip2px(15), 2));
        mThankAdapter = new ThankRecyAdapter(this, mThankList);
        thankLayout.setLayoutManager(thankManager);
        thankLayout.setAdapter(mThankAdapter);

        //排名榜单
        mRankAdapter = new DiscoveryRankAdapter(this,mList);
        rankLayout.setAdapter(mRankAdapter);
    }

    @Override
    public void onClick(View v) {
        if (v == backBtn) {//返回
            onBackPressed();
        }
    }

    /**
     * 获取公告、感谢数据
     */
    private void getNoticeThank() {
        NetworkManager.getNoticeThankInfo(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseNoticeThankSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //解析公告、感谢榜单接口返回的json数据
    private void parseNoticeThankSuc(String result) {
        NoticeThankBean bean = new Gson().fromJson(result, NoticeThankBean.class);
        mNoticeList.clear();
        mThankList.clear();
        mNoticeList.addAll(bean.getData().getNoticeList());
        mThankList.addAll(bean.getData().getThankList());

        mNoticeAdapter.notifyDataSetChanged();
        mThankAdapter.notifyDataSetChanged();
    }

    /**
     * 获取榜单榜首数据
     */
    private void getRanking() {
        NetworkManager.getRankingTopInfo(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseRankingSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseRankingSuc(String result) {
        DiscoveryRankingBean bean = new Gson().fromJson(result, DiscoveryRankingBean.class);
        mList.clear();
        mList.put(0,bean.getData().getDay());
        mList.put(1,bean.getData().getWeek());
        mList.put(2,bean.getData().getMonth());
        mRankAdapter.notifyDataSetChanged();

    }



}
